import {
  set_user_token
} from '../actions/passport';

import {
  set_user_info,
  set_user_avatar,
  set_user_nick,
} from '../actions/member';

function account (state = {
  uid: 0,
  organizeid: 0,
  timestamp: 0,
  token: "",
  avatar: null,
  email: "",
  nickname: "",
  roleList: []
}, action) {
  if (state.uid === 0) {
    state.uid = parseInt(window.localStorage.getItem("_uid"));
    state.organizeid = parseInt(window.localStorage.getItem("_organizeid"));
    state.timestamp = parseInt(window.localStorage.getItem("_timestamp"));
    state.token = window.localStorage.getItem("_token");
  }

  switch (action.type) {
    case set_user_token:
      return Object.assign({}, state, {
        uid: action.uid,
        organizeid: action.organizeid,
        timestamp: action.timestamp,
        token: action.token,
      });
    case set_user_info:
      let avatar = null;
      if (action.avatar !== null) {
        avatar = action.avatar
      }
      return Object.assign({}, state, {
        organizeName: action.organizeName,
        avatar: avatar,
        nickname: action.nickname,
        email: action.email,
        projectList: action.projectList,
        roleList: action.roleList,
      });
    case set_user_avatar:
      return Object.assign({}, state, {
        avatar: action.avatar,
      });
    case set_user_nick:
      return Object.assign({}, state, {
        nickname: action.nickname,
      });
    default:
      return state;
  }
}

export default account;